<?php
namespace addons\paotui\service;

class UserAddress
{

    public $model;

    public function __construct()
    {
        $this->model = new \app\admin\model\paotui\UserAddress();
    }

    public function refreshUpdateTime($ids,$user_id){
        $this->model->where(['id'=>['in',$ids],'user_id'=>$user_id])->update(['updatetime'=>time()]);
    }

    /**
     * 获取最后选择的发件地址
     * @param $user_id
     * @return array|bool|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLastFromAddress($user_id){
        $addressInfo = $this->model
            ->field("address.id,address.province,address.city,address.district,address.poi,address.lat,address.lng,address.address,address.name,address.mobile")
            ->alias("address")
            ->join("paotui_post post","address.id=post.from_address_id","inner")
            ->where(['address.user_id'=>$user_id,"post.user_id"=>$user_id])
            ->order("post.pay_status desc,post.paytime desc")
            ->find();
        if(!empty($addressInfo)){
            return $addressInfo;
        }

        return false;
    }

    /**
     * 获取用户地址列表
     * @param $where
     * @param $page
     * @param $limit
     * @param string $field
     * @param string $sort
     * @param string $search
     * @return array
     */
    public function getUserAddressList($where,$page,$limit,$field='*',$sort='a.id desc',$search=""){

        $_where = [];
        if($search){
            $_where = function ($query) use ($search){
                $query->where('name','like','%'.$search.'%')
                    ->whereOr('mobile','like','%'.$search.'%')
                    ->whereOr('poi','like','%'.$search.'%')
                    ->whereOr('address','like','%'.$search.'%');
            };
        }

        $total = $this->model->where($where)->where($_where)->count();

        if($page){
            $data = collection($this->model
                ->where($where)
                ->where($_where)
                ->page($page,$limit)
                ->field($field)->order($sort)->select())->toArray();
        }else{
            $data = collection($this->model
                ->where($where)
                ->where($_where)
                ->field($field)->order($sort)->select())->toArray();
        }

        return array('data'=>$data,'total'=>$total);
    }

    /**
     * 获取地址详情
     */
    public function getAddressInfo($id,$user_id){
		if($id){
			return $this->model->where(['id'=>$id,'user_id'=>$user_id])->find();
		}else{
			return $this->model->where(['user_id'=>$user_id])->order('is_default')->find();
		}
    }


    /**
     * 验证当前城市是否在运营范围内
     * @param $city
     * @return bool
     */
    public static function checkCity($city){
        $cityConfig = Config::get('city');
        $_city = empty($cityConfig)?[]:explode("/",$cityConfig);
        $_city = empty($_city)?"":$_city[1];
        if($city != $_city){
            return false;
        }
        return true;
    }
}